package com.bdjk.bdmw.InfoGath.ws.position.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.bdjk.bdmw.InfoGath.business.beianbei.model.BeiAnBeiPositionModel;
import com.bdjk.bdmw.InfoGath.business.beianbei.vo.BeiAnBeiPositionVO;
import com.bdjk.bdmw.InfoGath.common.dao.impl.JdbcDAOImpl;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;
import com.bdjk.bdmw.InfoGath.ws.position.dao.PositionDAO;
import com.bdjk.bdmw.InfoGath.ws.position.model.PositionModel;

public class PositionDAOImpl extends JdbcDAOImpl implements PositionDAO {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public List<PositionModel> queryHistoryPosition(List<Object> paramList) {
		// TODO Auto-generated method stub
		String sql=new String("SELECT * FROM t_position WHERE UNIX_TIMESTAMP( send_time ) BETWEEN UNIX_TIMESTAMP( ?) AND UNIX_TIMESTAMP( ? ) AND ter_ident=? ORDER BY send_time");
		List<PositionModel> result = null;
		try {
			result=query(sql,paramList);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public PositionModel queryCurrentPosition(List<Object> paramList) {
		// TODO Auto-generated method stub
	    String sql="SELECT * FROM (SELECT *  FROM t_position WHERE ter_ident=?)t ORDER BY send_time DESC LIMIT 0,1";
		
		PositionModel model=null;
		List<PositionModel> result;
		try {
			result = query(sql,paramList);
			if(result.size()>0){
				model=result.get(0);
			}
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return model;
	}

	
	public List<PositionModel> query(String sql,List<Object> paramList) throws BusinessException {
		List<Object> sqlrs = executeResultSQL(sql.toString(),paramList);
		List<PositionModel> result=new ArrayList<PositionModel>();
		for(int i=0;i<sqlrs.size();i++){
			PositionModel model=new PositionModel();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			if(null != rs.get(1)){
				model.setTerIdent(rs.get(1).toString());
			}
			if(null != rs.get(2)){
				model.setSendTime(sdf.format(rs.get(2)));
			}
			if(null != rs.get(3)){
				model.setLongitude(rs.get(3).toString());
			}
			if(null != rs.get(4)){
				model.setLatitude(rs.get(4).toString());
			}
			if(null != rs.get(5)){
				model.setAltitude(rs.get(5).toString());
			}
			if(null != rs.get(6)){
				model.setSpeed(rs.get(6).toString());
			}
			if(null != rs.get(7)){
				model.setDirection(rs.get(7).toString());
			}
			result.add(model);
		}
		return result;
	}
}
